package com.ylr.basic.framework.resolver;

import com.ylr.base.common.enums.AesEnum;
import com.ylr.base.common.exception.YlrException;
import com.ylr.base.common.tool.Result;
import com.ylr.basic.common.vo.AesVo;
import com.ylr.basic.framework.feign.client.YbAesClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2023-05-12 10:23:29
 * className: YbAesResolver
 * version: 1.0
 * description:
 */
@Component
public class YbAesResolver {
    
    private static final Logger log = LoggerFactory.getLogger(YbAesResolver.class);

    @Value("${spring.application.simple-name:ylr}")
    private String applicationSimpleName;
    
    private final YbAesClient ybAesClient;

    public YbAesResolver(YbAesClient ybAesClient) {
        this.ybAesClient = ybAesClient;
    }

    /**
     * 明文加密接口
     * @param plainText 明文
     * @param aesEnum   密文类型枚举对象
     * @return 密文
     */
    public String encrypt(String plainText, AesEnum aesEnum) {
        AesVo aesVo = new AesVo();
        aesVo.setValue(plainText)
                .setType(aesEnum.getValue());
        Result<String> result = ybAesClient.encrypt(aesVo);
        if (result.isFail()) {
            log.warn("{}模块 => 明文加密接口调用异常。message={}", applicationSimpleName, result.getMsg());
            throw new YlrException(result);
        }
        return result.getData();
    }

    /**
     * 明文加密接口
     * 生成十六进制密文
     * @param plainText 明文
     * @return 密文
     */
    public String encryptToHex(String plainText) {
        AesVo aesVo = new AesVo();
        aesVo.setValue(plainText)
                .setType(AesEnum.HEX.getValue());
        Result<String> result = ybAesClient.encrypt(aesVo);
//        Result<String> result = ybAesClient.encryptToHex(plainText);
        if (result.isFail()) {
            log.warn("{}模块 => 明文加密接口调用，生成十六进制密文异常。message={}", applicationSimpleName, result.getMsg());
            throw new YlrException(result);
        }
        return result.getData();
    }

    /**
     * AES密文解密接口
     * @param cipherText 密文
     * @return 明文
     */
    public String decodeHex(String cipherText) {
        AesVo aesVo = new AesVo();
        aesVo.setValue(cipherText)
                .setType(AesEnum.HEX.getValue());
        Result<String> result = ybAesClient.decode(aesVo);
//        Result<String> result = ybAesClient.decodeHex(cipherText);
        if (result.isFail()) {
            log.warn("{}模块 => AES密文解密接口调用，解密十六进制密文异常。message={}", applicationSimpleName, result.getMsg());
            throw new YlrException(result);
        }
        return result.getData();
    }

    /**
     * AES密文半解密接口
     * @param cipherText 密文
     * @param start      脱敏开始下标
     * @param end        脱敏结束下标
     * @return 明文
     */
    public String subDecodeHex(String cipherText, Integer start, Integer end) {
        AesVo aesVo = new AesVo();
        aesVo.setValue(cipherText)
                .setType(AesEnum.HEX.getValue())
                .setStart(start)
                .setEnd(end);
        Result<String> result = ybAesClient.decode(aesVo);
//        Result<String> result = ybAesClient.subDecodeHex(cipherText, start, end);
        if (result.isFail()) {
            log.warn("{}模块 => AES密文半解密接口调用，解密十六进制密文异常。message={}", applicationSimpleName, result.getMsg());
            throw new YlrException(result);
        }
        return result.getData();
    }

    /**
     * 明文加密接口
     * 生成Base64密文
     * @param plainText 明文
     * @return 密文
     */
    public String encryptToBase64(String plainText) {
        AesVo aesVo = new AesVo();
        aesVo.setValue(plainText)
                .setType(AesEnum.BASE64.getValue());
        Result<String> result = ybAesClient.encrypt(aesVo);
//        Result<String> result = ybAesClient.encryptToBase64(plainText);
        if (result.isFail()) {
            log.warn("{}模块 => 明文加密接口调用，生成Base64密文异常。message={}", applicationSimpleName, result.getMsg());
            throw new YlrException(result);
        }
        return result.getData();
    }

    /**
     * AES密文解密接口
     * @param cipherText 密文
     * @return 明文
     */
    public String decodeBase64(String cipherText) {
        AesVo aesVo = new AesVo();
        aesVo.setValue(cipherText)
                .setType(AesEnum.BASE64.getValue());
        Result<String> result = ybAesClient.decode(aesVo);
//        Result<String> result = ybAesClient.decodeBase64(cipherText);
        if (result.isFail()) {
            log.warn("{}模块 => AES密文解密接口，解密Base64密文异常。message={}", applicationSimpleName, result.getMsg());
            throw new YlrException(result);
        }
        return result.getData();
    }

    /**
     * AES密文半解密接口
     * @param cipherText 密文
     * @param start      脱敏开始下标
     * @param end        脱敏结束下标
     * @return 明文
     */
    public String subDecodeBase64(String cipherText, Integer start, Integer end) {
        AesVo aesVo = new AesVo();
        aesVo.setValue(cipherText)
                .setType(AesEnum.BASE64.getValue())
                .setStart(start)
                .setEnd(end);
        Result<String> result = ybAesClient.decode(aesVo);
//        Result<String> result = ybAesClient.subDecodeBase64(cipherText, start, end);
        if (result.isFail()) {
            log.warn("{}模块 => AES密文半解密接口，解密Base64密文异常。message={}", applicationSimpleName, result.getMsg());
            throw new YlrException(result);
        }
        return result.getData();
    }

    /**
     * AES密文解密接口
     * @param cipherText 密文
     * @param type       密文类型
     * @return 明文
     */
    public String decode(String cipherText, Integer type) {
        AesVo aesVo = new AesVo();
        aesVo.setValue(cipherText)
                .setType(type);
        Result<String> result = ybAesClient.decode(aesVo);
        if (result.isFail()) {
            log.warn("{}模块 => AES密文解密接口调用异常。message={}", applicationSimpleName, result.getMsg());
            throw new YlrException(result);
        }
        return result.getData();
    }

    /**
     * AES密文解密接口
     * @param cipherText 密文
     * @param type       密文类型
     * @param start      脱敏开始下标
     * @param end        脱敏结束下标
     * @return 明文
     */
    public String decode(String cipherText, Integer type, Integer start, Integer end) {
        AesVo aesVo = new AesVo();
        aesVo.setValue(cipherText)
                .setType(type)
                .setStart(start)
                .setEnd(end);
        Result<String> result = ybAesClient.decode(aesVo);
        if (result.isFail()) {
            log.warn("{}模块 => AES密文半解密接口调用异常。message={}", applicationSimpleName, result.getMsg());
            throw new YlrException(result);
        }
        return result.getData();
    }


}
